using Cysharp.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

/// <summary>
/// 设置管理器（用于保存设置、红点等）
/// </summary>
public class SettingManager: GameFrameClassBase
{
    private static string m_SettingsPath = Path.Combine(Application.persistentDataPath, "settings.json");
    public static Settings settings { get; private set; } = new Settings();

    public override UniTask InitAsync()
    {
        if (File.Exists(m_SettingsPath))
        {
            try
            {
                var text = File.ReadAllText(m_SettingsPath);
                settings = text.ToObject<Settings>();
            }
            catch (Exception ex)
            {
                CreateDefaultSettings();
                Debug.LogException(ex);
            }
        }
        else
        {
            CreateDefaultSettings();
        }
        return UniTask.CompletedTask;
    }


    private static List<Resolution> s_AvaliableResolutions;
    public static List<Resolution> avaliableResolutions
    {
        get
        {
            if (s_AvaliableResolutions == null)
            {
                s_AvaliableResolutions = new List<Resolution>();
                var systemResolutions = Screen.resolutions;
                if (systemResolutions != null && systemResolutions.Length > 1)
                {
                    s_AvaliableResolutions.AddRange(systemResolutions);
                }
                if (s_AvaliableResolutions.Count == 0)
                {
                    var screenWidth = Screen.width;
                    var screenHeight = Screen.height;
                    if (screenWidth < screenHeight)
                    {
                        var tempWidth = screenWidth;
                        screenWidth = screenHeight;
                        screenHeight = tempWidth;
                    }
                    if (screenWidth >= 1280)
                        s_AvaliableResolutions.Add(new Resolution { width = 1280, height = 720 });
                    if (screenWidth >= 1360)
                        s_AvaliableResolutions.Add(new Resolution { width = 1366, height = 768 });
                    if (screenWidth >= 1600)
                        s_AvaliableResolutions.Add(new Resolution { width = 1600, height = 900 });
                    if (screenWidth >= 1920)
                        s_AvaliableResolutions.Add(new Resolution { width = 1920, height = 1080 });
                    if (screenWidth > 1920)
                        s_AvaliableResolutions.Add(new Resolution { width = screenWidth, height = (int)(screenWidth / 16f * 9) });
                }
            }
            return s_AvaliableResolutions;
        }
    }

    private static void CreateDefaultSettings()
    {
        settings = new Settings();
    }

    public static void Save()
    {
        try
        {
            var text = settings.ToJson();
            File.WriteAllText(m_SettingsPath, text);
        }
        catch (Exception ex)
        {
            Debug.LogError("配置文件保存失败");
            Debug.LogException(ex);
        }
    }
}